package project.ccso.top.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.management.loading.PrivateClassLoader;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import project.ccso.top.bean.GridPageBean;
import project.ccso.top.bean.ReturnResult;
import project.ccso.top.entity.OneCategory;
import project.ccso.top.enumerate.Result;
import project.ccso.top.service.OneCategoryService;
import project.ccso.top.service.impl.OneCategoryServiceImpl;

/**
 * 
 * @author aibinxiao
 * @date 2018年8月23日 下午2:54:22
 */
@WebServlet("/OneCategoryServlet")
public class OneCategoryServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = LoggerFactory.getLogger(OneCategoryServlet.class);
	private static ObjectMapper mapper = new ObjectMapper();
	private OneCategoryService oneCategoryService = new OneCategoryServiceImpl();
	private OneCategory oneCategory = null;
	private List<OneCategory>list = null;
	
	//显示一级分类信息，支持搜索
	public void findByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String pageNo = request.getParameter("page"); // pageNum 页码
		String pageSize = request.getParameter("rows"); // pageSize 每页条数
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		GridPageBean<OneCategory>pageBean = null;
		try {
			pageBean = oneCategoryService.findByPage(pageNo, pageSize, id, name, code, startDate, endDate);
			result.setResult(Result.SUCCESS.name());
			result.setMessage("查询第" + pageNo + "页的一级分类信息成功！");
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		result.setData(pageBean);
		writer.print(mapper.writeValueAsString(result));
		
	}
	
	//编辑信息回显
	public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		int id = Integer.parseInt(request.getParameter("id"));
		try {
			oneCategory = oneCategoryService.findCate(id);
			result.setResult(Result.SUCCESS.name());
			result.setMessage("进入编辑编号为" + id + "的一级分类信息成功！");
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		logger.info("oneCategory");
		result.setData(oneCategory);
		writer.print(mapper.writeValueAsString(result));
	}
	//修改和增加一级分类信息
	public void oneCateUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		ReturnResult result = new ReturnResult();
		String id = request.getParameter("id");
		System.out.println(id);
		String code = request.getParameter("code");
		String name = request.getParameter("name");
		String desc = request.getParameter("desc");
		boolean boolResult = false;
		try {	
			 boolResult = oneCategoryService.UpdateCate(id,code,name,desc);
			 if (boolResult) {
				result.setResult(Result.SUCCESS.name());
				if(StringUtils.isEmpty(id)) {
					result.setMessage("添加名称为" + name + "的一级分类信息成功！");
				}else {
					result.setMessage("修改Id为" + id + "的一级分类信息成功！");
				}
				logger.info(result.getMessage());
			} 
		} catch (Exception e) {
			result.setResult(Result.FAILED.name());
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		logger.info("boolResult");
		result.setData(boolResult);
		writer.print(mapper.writeValueAsString(result));
	}

}
